﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Win8Start
{
	public partial class StartMenu : Form
	{
		public StartMenu()
		{
			InitializeComponent();
		}

		void SetWindowLocation()
		{
			if (this.WindowState == FormWindowState.Normal)
			{
				this.Left = 0;

				int h = 0;
				h += PinnedApps.TotalHeight;
				h += RecentApps.TotalHeight;
				h += 70;  // account for All programs, search box, and splitters
				this.Height = h;

				this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
			}
		}

		private void Form1_ResizeEnd(object sender, EventArgs e)
		{
			SetWindowLocation();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			SetWindowLocation();
			LoadAll();
		}

		private void Form1_Deactivate(object sender, EventArgs e)
		{
			HideMenu();
		}

		public void ClearAll()
		{
			AllApps.Clear();
			PinnedApps.Clear();
			RightPanel.Clear();
			RecentApps.Clear();
		}

		public void LoadAll()
		{
			PinnedApps.FolderList.Add(@"%APPDATA%\Microsoft\Internet Explorer\Quick Launch\User Pinned\StartMenu");
			PinnedApps.ReloadAll();

			RightPanel.Add("1", "explorer.exe", "My Computer", Icon.ExtractAssociatedIcon(@"C:\windows\explorer.exe"));
			RightPanel.ReloadAll();


			AllApps.FolderList.Add(@"%APPDATA%\Microsoft\Windows\Start Menu\Programs");
			AllApps.FolderList.Add(@"%ProgramData%\Microsoft\Windows\Start Menu\Programs");
			AllApps.ReloadAll();

			SetWindowLocation();
		}

		private void StartMenu_VisibleChanged(object sender, EventArgs e)
		{
		}

		private void StartMenu_Activated(object sender, EventArgs e)
		{
		}

		private void AllProgramsLabel_Click(object sender, EventArgs e)
		{
			AllApps.BringToFront();
			AllApps.Visible = !AllApps.Visible;
			if (AllApps.Visible)
			{
				AllProgramsLabel.Text = "◄ Back";
			}
			else
			{
				AllProgramsLabel.Text = "► All Programs";
			}
		}

		private void StartMenu_KeyPress(object sender, KeyPressEventArgs e)
		{
		}

		private void StartMenu_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
				HideMenu();
		}

		private void closeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void shutDownToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Shell32.Shell shell = new Shell32.Shell();
			shell.ShutdownWindows();
		}

		private void AllApps_ItemSelected(object sender, UI.ShortcutPanel.MenuItemEventArgs e)
		{
			AllApps.Visible = false;
			HideMenu();
			WindowState = FormWindowState.Minimized;
			e.MenuItem.Shortcut.Launch();
		}

		public void HideMenu()
		{
			WindowState = FormWindowState.Minimized;
		}


	}
}
